//
//  EditViewController.m
//  WeiMo
//
//  Created by weixian he on 12-9-11.
//  Copyright (c) 2012年 . All rights reserved.
//

#import "EditViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "StringInputTableViewCell.h"
#import "DateInputTableViewCell.h"
#import "IntegerInputTableViewCell.h"
#import "TextFieldTableViewCell.h"
#import "FMManager.h"
#import "InterfaceDefine.h"
#import "ThreeImageCell.h"
#import "ThreeImageCell.h"
#import "DLog.h"
#import "SDImageCache.h"
#import "KeyValueObject.h"
#import "TouchImageView.h"
#import "TextFieldTableViewCell.h"
#import "SVSegmentedControl.h"
#import "InterfaceDefine.h"
#import "JSONKit.h"
#import "FMManager.h"
#import "AGImagePickerController.h"
#import "AGIPCToolbarItem.h"
#import "PersonalHeadShowCell.h"

#import <AssetsLibrary/AssetsLibrary.h>
#import "DemoImageEditor.h"
#import "UIImageView+WebCache.h"
#import "SFHFKeychainUtils.h"


#define kImageIntNums 4
#define kImageViewTagBase 10

#define kTextFieldTagBase 1000

@interface EditViewController ()
{
    UITextField *mCurTextField;
    int mTextFieldTagIndex;
    int mSex; //性别  1 男  2女
    MBProgressHUD *mHUD;
    int mSelDeleteImageIndex;
}

@property(nonatomic,retain) UIView *section0footView;

@property(nonatomic,retain) NSMutableArray *section0list;//图片
@property(nonatomic,retain) NSMutableArray *section1List;//个人介绍
@property(nonatomic,retain) NSMutableArray *textFieldList1;//个人介绍

@property(nonatomic,copy) NSString *modelsStyle;//个人风格
@property(nonatomic,copy) NSString *modelsPersent;//个人/公司介绍
@property(nonatomic,copy) NSString *modelsExperience;//演出经历
@property(nonatomic,copy) NSString *modelsPhotostr; //图片id
@property(nonatomic,copy) NSString *modelsAvatarstr;

@property(nonatomic,retain) NSMutableArray *section4List;//工作联系方式
@property(nonatomic,retain) NSMutableArray *inputList;

@property(nonatomic,retain) UILabel *section2headerView;
@property(nonatomic,retain) UILabel *section3headerView;
@property(nonatomic,retain) UILabel *section4headerView;
@property(nonatomic,retain) UILabel *section5headerView;

@property(nonatomic,retain) NSMutableDictionary *editboxDic;
@property(nonatomic,retain) SVSegmentedControl *sVSegmentedControl;

@property(nonatomic,retain) NSMutableDictionary *saveDataDic;

@property(nonatomic, retain) NSMutableArray *selectedPhotos; //选择图片

@property(nonatomic,retain) DemoImageEditor *imageEditor;
@property(nonatomic,retain) ALAssetsLibrary *library;
@property(nonatomic,retain) UIImage *saveImage;

@property (nonatomic,retain) NSString *userIdentifyRemind;
@property (nonatomic,retain) NSString *modelsFirstTypeRemind;
@property (nonatomic,retain) NSString *modelsSecondTypeRemind;
@property (nonatomic,retain) NSString *modelFirstTypeString;
@property (nonatomic,retain) NSString *modelSecondTypeString;
@end


@implementation EditViewController
@synthesize dataDic;
@synthesize delegate;

@synthesize section0footView;

@synthesize section0list,section1List,textFieldList1;

@synthesize modelsStyle,modelsPersent,modelsExperience,modelsAvatarstr,modelsPhotostr;

@synthesize inputList,section4List;

@synthesize section2headerView,section3headerView,section4headerView,section5headerView;

@synthesize editboxDic,saveDataDic;
@synthesize sVSegmentedControl;

@synthesize selectedPhotos;
@synthesize library = _library;
@synthesize imageEditor = _imageEditor;
@synthesize saveImage = _saveImage;

@synthesize userIdentifyRemind,modelsFirstTypeRemind,modelsSecondTypeRemind;


- (void)dealloc
{
    self.dataDic = nil;
    self.delegate = nil;
    
    self.section0footView = nil;
    
    self.section0list = nil;
    self.section1List = nil;
    self.textFieldList1 = nil;
    
    self.modelsPersent = nil;
    self.modelsStyle = nil;
    self.modelsExperience = nil;
    self.modelsPhotostr = nil;
    self.modelsAvatarstr = nil;
    
    self.inputList = nil;
    self.section4List = nil;
    
    self.section2headerView = nil;
    self.section3headerView = nil;
    self.section4headerView = nil;
    self.section5headerView = nil;
    
    self.editboxDic = nil;
    self.saveDataDic = nil;
    
    self.sVSegmentedControl = nil;
    
    self.selectedPhotos = nil;
    self.library = nil;
    self.imageEditor = nil;
    self.saveImage = nil;
    
    self.userIdentifyRemind = nil;
    self.modelsFirstTypeRemind = nil;
    self.modelsSecondTypeRemind = nil;
    self.modelFirstTypeString = nil;
    self.modelSecondTypeString = nil;
    
    [super dealloc];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[UIBarButtonItem appearance] setTintColor:[UIColor blackColor]];
    
    self.modelFirstTypeString = @"";
    self.modelSecondTypeString = @"";
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleBordered target:self action:@selector(editedAndToSaveDatas:)];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    [rightButtonItem release];
    
    self.userIdentifyRemind = [[FMManager sharedInstance] modeIdentify];
    
    self.tableView.backgroundColor = [UIColor underPageBackgroundColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self initSectionHeaderView];
    [self initSections];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    FMManager *fm = [FMManager sharedInstance];
    if(fm.updateSecondCtrl)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    [self hideTabBar];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - private methods

//隐藏TabBar
- (void)hideTabBar
{
    if (self.tabBarController.tabBar.hidden == YES)
    {
        return;
    }
    UIView *contentView;
    if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    else
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    contentView.frame = CGRectMake(contentView.bounds.origin.x,  contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);
    self.tabBarController.tabBar.hidden = YES;
}

//显示TabBar
- (void)showEditMessageTabBar
{
    if (self.tabBarController.tabBar.hidden == NO)
    {
        return;
    }
    UIView *contentView;
    if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]])
        
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    
    else
        
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height - self.tabBarController.tabBar.frame.size.height);
    self.tabBarController.tabBar.hidden = NO;
    
}

-(void)initSectionHeaderView
{
    self.section2headerView = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 28)] autorelease];
    self.section2headerView.text = @"  个人风格:";
    self.section2headerView.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:223.0/255.0 blue:223.0/255.0 alpha:1];
    self.section2headerView.textColor = [UIColor colorWithRed:65.0/255 green:65.0/255 blue:65.0/255 alpha:1];
    
    self.section3headerView = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 28)] autorelease];
    self.section3headerView.text = @"  个人/企业介绍:";
    self.section3headerView.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:223.0/255.0 blue:223.0/255.0 alpha:1];
    self.section3headerView.textColor = [UIColor colorWithRed:65.0/255 green:65.0/255 blue:65.0/255 alpha:1];
    
    
    self.section4headerView = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 28)] autorelease];
    self.section4headerView.text = @"  联系方式(发送资料显示):";
    self.section4headerView.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:223.0/255.0 blue:223.0/255.0 alpha:1];
    self.section4headerView.textColor = [UIColor colorWithRed:65.0/255 green:65.0/255 blue:65.0/255 alpha:1];
    
    self.section5headerView = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 28)] autorelease];
    self.section5headerView.text = @"  演出经历/企业主营:";
    self.section5headerView.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:223.0/255.0 blue:223.0/255.0 alpha:1];
    self.section5headerView.textColor = [UIColor colorWithRed:65.0/255 green:65.0/255 blue:65.0/255 alpha:1];
    
    /*
     self.headerView2 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 28)] autorelease];
     self.headerView2.text = @"   风格特点";
     self.headerView2.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:223.0/255.0 blue:223.0/255.0 alpha:1];
     self.headerView2.textColor = [UIColor colorWithRed:65.0/255 green:65.0/255 blue:65.0/255 alpha:1];
     */
}

- (void)initSections
{
    //FMManager *fm = [FMManager sharedInstance];
    self.inputList = [[[NSMutableArray alloc] init] autorelease];
    
    //section 0 图片
    self.saveDataDic = self.dataDic;
    self.modelsPhotostr = [self.dataDic objectForKey:@"modelsPhotostr"];
    
    DLog(@"modelsPhotostr= %@",self.modelsPhotostr);
    NSMutableArray *tempArray = [[[NSMutableArray alloc] init] autorelease];
    if(![self.modelsPhotostr isEqualToString:@""])
    {
        NSRange range = [self.modelsPhotostr rangeOfString:@";"];
        if (range.location != NSNotFound)
        {
            NSArray *strPhotos = [self.modelsPhotostr componentsSeparatedByString:@";"];
            
            /*
             canClick:推荐
             isCheck:预约
             key:用户id
             keyDispName:小图url
             */
            for( int i=0; i<strPhotos.count; i++)
            {
                KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
                item.canClick = YES;
                item.isCheck = YES;
                
                item.key = [strPhotos objectAtIndex:i];
                //NSString *imageUrl = [NSString stringWithFormat:@"%@/%@/%@/%@",kHttpHost,@"photo",fm.userId,item.key];
                //DLog(@"imageUrl= %@",imageUrl);
                //item.keyDispName = imageUrl;
                [tempArray addObject:item];
            }
        }
        else
        {
            KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
            item.canClick = YES;
            item.isCheck = YES;
            
            item.key = self.modelsPhotostr;
            //NSString *imageUrl = [NSString stringWithFormat:@"%@/%@/%@/%@",kHttpHost,@"photo",fm.userId,item.key];
            //DLog(@"imageUrl= %@",imageUrl);
            //item.keyDispName = imageUrl;
            [tempArray addObject:item];
        }
    }
    
    self.section0list = tempArray;
    
    
    if(self.section0list.count < 8)
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"add";
        item.keyDispName = @"addphotos.png";
        [self.section0list addObject:item];
    }
    
    
    //section1 个人介绍
    self.section1List = [[[NSMutableArray alloc] init] autorelease];
    self.textFieldList1 = [[[NSMutableArray alloc] init] autorelease];
    
    NSString *modelFirstType = [self.dataDic objectForKey:@"modelsFirstType"];
    self.modelsFirstTypeRemind = modelFirstType;
    modelFirstType = [FMManager getMenuNameOfId:modelFirstType];
    self.modelFirstTypeString = modelFirstType;
    
    NSString *modelSecondType = [self.dataDic objectForKey:@"modelsType"];
    NSRange range = [modelSecondType rangeOfString:@","];
    if (range.location != NSNotFound)
    {
        NSArray *typeArray = [modelSecondType componentsSeparatedByString:@","];
        if (typeArray.count > 0)
        {
            modelSecondType = [typeArray objectAtIndex:0];
            if (modelSecondType.length > 0)
            {
                self.modelsSecondTypeRemind = modelSecondType;
                modelSecondType = [FMManager getMenuNameOfId:modelSecondType];
            }
            
            for (int i=1; i<typeArray.count; i++)
            {
                NSString *otherType = [typeArray objectAtIndex:i];
                if (otherType.length > 0)
                {
                    self.modelsSecondTypeRemind = [NSString stringWithFormat:@"%@,%@",self.modelsSecondTypeRemind,otherType];
                    otherType = [FMManager getMenuNameOfId:otherType];
                    modelSecondType = [NSString stringWithFormat:@"%@,%@",modelSecondType,otherType];
                }
            }
        }
    }
    self.modelSecondTypeString = modelSecondType;
    
    //身份属性
    [self.section1List addObject:@"身份:"];
    
    //昵称
    [self.section1List addObject:@"名称:"];
    CGRect rect = CGRectMake(60, 4, 140, 28);
    NSString *modelsName = [self.dataDic objectForKey:@"modelsName"];
    mTextFieldTagIndex = kTextFieldTagBase;
    {
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = modelsName;
        textField.delegate = self;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [self.textFieldList1 addObject:textField];
    }
    
    //性别
    // mTextFieldTagIndex++;
    [self.section1List addObject:@"性别:"];
    NSNumber *modelsSex = [self.dataDic objectForKey:@"modelsSex"];
    if(modelsSex!=nil && modelsSex.intValue == 1)
    {
        mSex =1;
    }
    else
    {
        mSex=2;
    }
    
    
    self.sVSegmentedControl = [[[SVSegmentedControl alloc] initWithSectionTitles:[NSArray arrayWithObjects:@"女", @"男", nil]]autorelease];
    self.sVSegmentedControl.changeHandler = ^(NSUInteger newIndex) {
        //NSLog(@"segmentedControl did select index %i (via block handler)", newIndex);
        if(newIndex == 0)
        {
            mSex =2;
        }
        else if(newIndex == 1)
        {
            mSex =1;
        }
        
    };
    
    
    if(mSex == 1)
    {
        self.sVSegmentedControl.selectedIndex = 1;
    }
    else
    {
        self.sVSegmentedControl.selectedIndex = 0;
    }
    
    self.sVSegmentedControl.center = CGPointMake(100, 18);
    //self.sVSegmentedControl.thumb.tintColor = [UIColor colorWithRed:0.6 green:0.2 blue:0.2 alpha:1];
    
    //职业
    [self.section1List addObject:@"职业:"];
    NSString *modelsBloodtype = [self.dataDic objectForKey:@"modelsBloodtype"];
    {
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = modelsBloodtype;
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [self.textFieldList1 addObject:textField];
    }
    
    //身高
    {
        [self.section1List addObject:@"身高:"];
        NSString *height = [self.dataDic objectForKey:@"modelsHeight"];
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = height;
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.placeholder=@"cm";
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        [self.textFieldList1 addObject:textField];
    }
    
    //体重
    {
        [self.section1List addObject:@"体重:"];
        NSString *temp = [self.dataDic objectForKey:@"modelsWeight"];
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = temp;
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.placeholder=@"kg";
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        [self.textFieldList1 addObject:textField];
    }
    
    //三围
    {
        [self.section1List addObject:@"三围:"];
        NSString *temp = [self.dataDic objectForKey:@"modelsMeasurements"];
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = temp;
        textField.placeholder = @"88,70,90";
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        [self.textFieldList1 addObject:textField];
    }
    
    //鞋码
    {
        [self.section1List addObject:@"鞋码:"];
        NSString *temp = [self.dataDic objectForKey:@"modelsSize"];
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = temp;
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.placeholder= @"41";
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [self.textFieldList1 addObject:textField];
    }
    
    //肩宽
    {
        [self.section1List addObject:@"肩宽:"];
        NSString *temp = [self.dataDic objectForKey:@"modelsJks"];
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = temp;
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.placeholder = @"80";
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [self.textFieldList1 addObject:textField];
    }
    
    //现居城市
    {
        rect.origin.x = rect.origin.x + 20;
        rect.size.width = rect.size.width - 20;
        [self.section1List addObject:@"所在城市:"];
        NSString *temp = [self.dataDic objectForKey:@"modelsAddress"];
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = temp;
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        [self.textFieldList1 addObject:textField];
    }
    
    //所属公司市
    {
        rect.size.width = rect.size.width + 100;
        [self.section1List addObject:@"所属公司:"];
        NSString *temp = [self.dataDic objectForKey:@"modelsSsgs"];
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = temp;
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        [self.textFieldList1 addObject:textField];
    }
    
    
    //
    self.modelsStyle = [self.dataDic objectForKey:@"modelsStyle"];
    {
        static NSString *Cell2 = @"Cell2";
        TextFieldTableViewCell *cell = [[[TextFieldTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Cell2] autorelease];
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textField.layer.cornerRadius=4.0f;
        cell.textField.layer.masksToBounds=YES;
        cell.textField.backgroundColor=[UIColor lightGrayColor];
        cell.textField.layer.borderColor=[[UIColor grayColor]CGColor];
        cell.textField.layer.borderWidth= 1.0f;
        cell.textField.text = self.modelsStyle;
        cell.textField.placeholder = @"最多500字符";
        [self.inputList addObject:cell];
    }
    
    //
    self.modelsPersent = [self.dataDic objectForKey:@"modelsPersent"];
    {
        static NSString *Cell3 = @"Cell3";
        TextFieldTableViewCell *cell = [[[TextFieldTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Cell3] autorelease];
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textField.layer.cornerRadius=4.0f;
        cell.textField.layer.masksToBounds=YES;
        cell.textField.backgroundColor=[UIColor lightGrayColor];
        cell.textField.layer.borderColor=[[UIColor grayColor]CGColor];
        cell.textField.layer.borderWidth= 1.0f;
        cell.textField.text = self.modelsPersent;
        cell.textField.placeholder = @"最多500字符";
        [self.inputList addObject:cell];
    }
    
    
    self.section4List = [[[NSMutableArray alloc] init] autorelease];
    //工作电话
    {
        rect.size.width = rect.size.width - 50;
        [self.section4List addObject:@"工作电话:"];
        NSString *temp = [self.dataDic objectForKey:@"modelsPhone"];
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = temp;
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [self.textFieldList1 addObject:textField];
    }
    
    //工作邮箱
    {
        [self.section4List addObject:@"工作邮箱:"];
        NSString *temp = [self.dataDic objectForKey:@"modelsEmail"];
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = temp;
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [self.textFieldList1 addObject:textField];
    }
    
//    //工作QQ
//    {
//        [self.section4List addObject:@"工作QQ:"];
//        NSString *temp = [self.dataDic objectForKey:@"modelsQq"];
//        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
//        textField.text = temp;
//        textField.delegate = self;
//        mTextFieldTagIndex++;
//        textField.tag = mTextFieldTagIndex;
//        textField.borderStyle=UITextBorderStyleNone;
//        textField.layer.cornerRadius=4.0f;
//        textField.layer.masksToBounds=YES;
//        textField.backgroundColor=[UIColor lightGrayColor];
//        textField.layer.borderColor=[[UIColor grayColor]CGColor];
//        textField.layer.borderWidth= 1.0f;
//        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
//        textField.returnKeyType = UIReturnKeyNext;
//        [self.textFieldList1 addObject:textField];
//    }
//    
//    //工作微信
//    {
//        [self.section4List addObject:@"工作微信:"];
//        NSString *temp = [self.dataDic objectForKey:@"modelsVx"];
//        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
//        textField.text = temp;
//        textField.delegate = self;
//        mTextFieldTagIndex++;
//        textField.tag = mTextFieldTagIndex;
//        textField.borderStyle=UITextBorderStyleNone;
//        textField.layer.cornerRadius=4.0f;
//        textField.layer.masksToBounds=YES;
//        textField.backgroundColor=[UIColor lightGrayColor];
//        textField.layer.borderColor=[[UIColor grayColor]CGColor];
//        textField.layer.borderWidth= 1.0f;
//        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
//        textField.returnKeyType = UIReturnKeyNext;
//        [self.textFieldList1 addObject:textField];
//    }
    
    //
    self.modelsExperience = [self.dataDic objectForKey:@"modelsExperience"];
    {
        static NSString *Cell5 = @"Cell5";
        TextFieldTableViewCell *cell = [[[TextFieldTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Cell5] autorelease];
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textField.layer.cornerRadius=4.0f;
        cell.textField.layer.masksToBounds=YES;
        cell.textField.backgroundColor=[UIColor lightGrayColor];
        cell.textField.layer.borderColor=[[UIColor grayColor]CGColor];
        cell.textField.layer.borderWidth= 1.0f;
        cell.textField.text = self.modelsExperience;
        cell.textField.placeholder = @"最多500字符";
        [self.inputList addObject:cell];
    }
    
    
    /*
     UITextField *textField;
     CGRect rect = CGRectMake(2, 2, 200, 30);
     textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
     //[textField setBorderStyle:UITextBorderStyleLine]; //外框类型
     textField.borderStyle=UITextBorderStyleNone;
     textField.layer.cornerRadius=4.0f;
     textField.layer.masksToBounds=YES;
     textField.backgroundColor=[UIColor orangeColor];
     textField.layer.borderColor=[[UIColor blackColor]CGColor];
     textField.layer.borderWidth= 1.0f;
     
     textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
     textField.placeholder = @"昵称:";
     [self.section0footView addSubview:textField];
     [self.editboxDic setObject:textField forKey:@"modelsName"];
     */
}

//正则表达式匹配
- (BOOL)predicateMatchFormate:(NSString *)formate withObject:(NSString *)string
{
    NSString * regex   = formate; //@"(^[0-9]{1,3}$)"
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch       = [pred evaluateWithObject:string];
    return isMatch;
}

#pragma mark - event

- (void)editedAndToSaveDatas:(id)sender
{
    NSString *modeIdentify = [[FMManager sharedInstance] modeIdentify];
    if ([modeIdentify isEqualToString:@"1"] || [modeIdentify isEqualToString:@"2"])
    {
        if (!self.modelsSecondTypeRemind || self.modelsSecondTypeRemind.length == 0)
        {
            [FMManager showAlertView:@"身份不能为空"];
            return;
        }
    }
    
    NSMutableDictionary *dic = [[[NSMutableDictionary alloc] init] autorelease];
    UITextField *textField;
    int index = 0;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsName = textField.text;
    if([modelsName isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"名称不能为空"];
        return;
    }
    [dic setObject:modelsName forKey:@"modelsName"];
    
    [dic setObject:[NSNumber numberWithInt:mSex] forKey:@"modelsSex"];
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsBloodtype = textField.text;
    if([modelsBloodtype isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"职业不能为空"];
        return;
    }
    [dic setObject:modelsBloodtype forKey:@"modelsBloodtype"];
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsHeight = textField.text;
    if(!modelsHeight || [modelsHeight isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"身高不能为空"];
        return;
    }
    else
    {
        NSString *prefor = @"(^[0-9]{1,3}$)";
        
        BOOL preResult = [self predicateMatchFormate:prefor withObject:modelsHeight];
        if (preResult)
        {
            [dic setObject:modelsHeight forKey:@"modelsHeight"];
        }
        else
        {
            [FMManager showAlertView:@"身高为1-3位数字"];
            return;
        }
    }
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsWeight = textField.text;
    if(!modelsWeight || [modelsWeight isEqualToString:@""])
    {
        [FMManager showAlertView:@"体重不能为空"];
        return;
    }
    else
    {
        NSString *prefor = @"(^[0-9]{1,3}$)";
        
        BOOL preResult = [self predicateMatchFormate:prefor withObject:modelsWeight];
        if (preResult)
        {
            [dic setObject:modelsWeight forKey:@"modelsWeight"];
        }
        else
        {
            [FMManager showAlertView:@"体重为1-3位数字"];
            return;
        }
    }
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsMeasurements = textField.text;
    if(!modelsMeasurements || [modelsMeasurements isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"三围不能为空"];
        return;
    }
    [dic setObject:modelsMeasurements forKey:@"modelsMeasurements"];
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsSize = textField.text;
    if(!modelsSize || [modelsSize isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"鞋码不能为空"];
        return;
    }
    else
    {
        NSString *prefor = @"(^[0-9]{1,2}$)";
        
        BOOL preResult = [self predicateMatchFormate:prefor withObject:modelsSize];
        if (preResult)
        {
            [dic setObject:modelsSize forKey:@"modelsSize"];
        }
        else
        {
            [FMManager showAlertView:@"鞋码为1-2位数字"];
            return;
        }
    }
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsJks = textField.text;
    if(!modelsJks || [modelsJks isEqualToString:@""])
    {
        [FMManager showAlertView:@"肩宽不能为空"];
        return;
    }
    else
    {
        NSString *prefor = @"(^[0-9]{1,3}$)";
        BOOL isMatch = [self predicateMatchFormate:prefor withObject:modelsJks];
        if (isMatch)
        {
            [dic setObject:modelsJks forKey:@"modelsJks"];
        }
        else
        {
            [FMManager showAlertView:@"肩宽为1-3位数字"];
            return;
        }
    }
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsAddress = textField.text;
    if([modelsAddress isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"所在城市不能为空"];
        return;
    }
    [dic setObject:modelsAddress forKey:@"modelsAddress"];
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsSsgs = textField.text;
    /*
    if([modelsAddress isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"所属公司不能为空"];
        return;
    }
     */
    [dic setObject:modelsSsgs forKey:@"modelsSsgs"];
    
    
    int inputTextIndex=0;
    UIPlaceHolderTextView *textView;
    TextFieldTableViewCell *cell = [self.inputList objectAtIndex:inputTextIndex];
    textView= cell.textField;
    NSString *modelsStyleStr = textView.text;
    if(!modelsStyleStr || [modelsStyleStr isEqualToString:@""])
    {
        [FMManager showAlertView:@"个人风格不能为空"];
        return;
    }
    else
    {
        if (modelsStyleStr.length > 500)
        {
            [FMManager showAlertView:@"个人风格不能超过500字符"];
            return;
        }
        else
        {
            [dic setObject:modelsStyleStr forKey:@"modelsStyle"];
        }
    }
    
    inputTextIndex++;
    cell = [self.inputList objectAtIndex:inputTextIndex];
    textView= cell.textField;
    NSString *modelsPersentStr = textView.text;
    if(!modelsPersentStr || [modelsPersentStr isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"个人/企业介绍不能为空"];
        return;
    }
    else
    {
        if (modelsPersentStr.length > 500)
        {
            [FMManager showAlertView:@"个人/企业介绍不能超过500字符"];
            return;
        }
        else
        {
            [dic setObject:modelsPersentStr forKey:@"modelsPersent"];
        }
    }
    
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsPhone = textField.text;
    if(!modelsPhone || [modelsPhone isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"工作电话不能为空"];
        return;
    }
    else
    {
        if (modelsPhone.length < 20)
        {
            [dic setObject:modelsPhone forKey:@"modelsPhone"];
        }
        else
        {
            [FMManager showAlertView:@"工作电话为少于20位的数字"];
            return;
        }
    }
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsEmail = textField.text;
    if(!modelsEmail || [modelsEmail isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"工作邮箱不能为空"];
        return;
    }
    else
    {
        NSString *prefor = @"(^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$)";
        BOOL isMatch = [self predicateMatchFormate:prefor withObject:modelsEmail];
        if (isMatch)
        {
            [dic setObject:modelsEmail forKey:@"modelsEmail"];
        }
        else
        {
            [FMManager showAlertView:@"邮箱格式不争取,请正确输入"];
            return;
        }
    }
    
//    index++;
//    textField= [self.textFieldList1 objectAtIndex:index];
//    NSString *modelsQq = textField.text;
//    if([modelsQq isEqualToString:@""])
//    {
//        [FMManager showAlertViewTitle:@"" message:@"工作QQ不能为空"];
//        return;
//    }
//    [dic setObject:modelsQq forKey:@"modelsQq"];
//    
//    index++;
//    textField= [self.textFieldList1 objectAtIndex:index];
//    NSString *modelsVx = textField.text;
//    if([modelsVx isEqualToString:@""])
//    {
//        [FMManager showAlertViewTitle:@"" message:@"工作微信不能为空"];
//        return;
//    }
//    [dic setObject:modelsVx forKey:@"modelsVx"];
    
    
    inputTextIndex++;

    cell = [self.inputList objectAtIndex:inputTextIndex];
    textView= cell.textField;
    NSString *modelsExperienceStr = textView.text;
    /*
    if([modelsExperience isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"演出经历不能为空"];
        return;
    }
     */
    [dic setObject:modelsExperienceStr forKey:@"modelsExperience"];

   
    [dic setObject:[self.dataDic objectForKey:@"userId"] forKey:@"userId"];
    [dic setObject:[self.dataDic objectForKey:@"modelsId"] forKey:@"modelsId"];
    
    [dic setObject:self.modelsFirstTypeRemind forKey:@"modelsFirstType"];
    
    self.modelsSecondTypeRemind = [NSString stringWithFormat:@"%@,",self.modelsSecondTypeRemind];
    [dic setObject:self.modelsSecondTypeRemind forKey:@"modelsType"];
    NSString *touxiang =  [self.saveDataDic objectForKey:@"modelsAvatarstr"];
    if(touxiang && touxiang.length > 0)
    {
        [dic setValue:touxiang forKey:@"modelsAvatarstr"];
    }
    
    NSString *ps = [self.saveDataDic objectForKey:@"modelsPhotostr"];
    if(ps && ps.length>0)
    {
        [dic setValue:ps forKey:@"modelsPhotostr"];
    }
    self.saveDataDic = dic;
    NSString *postStr = [dic JSONString];
    DLog(@"%@",postStr);
    
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",kHttpHost,kUpdateModelsInfo];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setRequestMethod:@"POST"];
    request.delegate = self;
    [request startAsynchronous];
    
    mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:mHUD];
    mHUD.delegate = self;
    [mHUD show:YES];
}

#pragma mark - Table view data source

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section == 2)
    {
        return self.section2headerView;
    }
    else if(section == 3)
    {
         return self.section3headerView;
    }
    else if(section == 4)
    {
        return self.section4headerView;
    }
    else if(section == 5)
    {
        return self.section5headerView;
    }
    
    return nil;
}

-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 2)
    {
        return self.section2headerView.frame.size.height;
    }
    else if(section == 3)
    {
         return self.section3headerView.frame.size.height;
    }
    else if(section == 4)
    {
       return self.section4headerView.frame.size.height;
    }
    else if(section == 5)
    {
        return self.section5headerView.frame.size.height;
    }
    
    return 0.0;
}


-(float) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if( indexPath.section == 0)
    {
        float space = (indexPath.row == 0) ? 6.0 : 4.0;
        return ((tableView.frame.size.width - 10) / 4.0 + space);
    }
    else if(indexPath.section == 1 || indexPath.section == 4)
    {
        return 36;
    }
    else if(indexPath.section == 2 || indexPath.section == 3)
    {
        return 88;
    }
    else if(indexPath.section == 5)
    {
        return 140;
    }
    
    return 44;
}

-(float) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    /*
    if(section == 0)
    {
        return self.section0footView.frame.size.height;
    }
     */
    
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    /*
    FMManager *fm = [FMManager sharedInstance];
    if( [fm.modeIdentify isEqualToString:@"1"] || [fm.modeIdentify isEqualToString:@"2"])
    {
        return 6;
    }
    else
    {
        return 7;
    }
     */
    
    return 6;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if(section == 0)
    {
        int temp =0;
        FMManager *fm = [FMManager sharedInstance];
        if( [fm.modeIdentify isEqualToString:@"3"] || [fm.modeIdentify isEqualToString:@"4"])
        {
            temp=1;
        }
        NSArray *array = self.section0list;
        int mol = array.count % kImageIntNums;
        int size = ([array count] )/kImageIntNums + 1;
        if(mol == 0)
        {
            size--;
        }
        DLog(@"section0list section = %d,size = %d",size,array.count);
        return (size + temp);
    }
    else if(section == 1)
    {
        return self.section1List.count;
    }
    else if(section == 2 || section == 3 || section == 5)
    {
        return 1;
    }
    else if(section == 4)
    {
        return self.section4List.count;
    }
    
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    /*
    if(section == 0)
    {
        return self.section0footView;
    }
    */
    
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = [indexPath row];
    int section = [indexPath section];
    DLog(@"section = %d",section);
    
    if(section == 0)
    {
            static NSString *CellIdentifier0 = @"Cell0";
            ThreeImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
            if(cell == nil)
            {
                cell = [[[ThreeImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0 canCheck:NO] autorelease];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            NSArray *array = self.section0list;
            int index;
            int listSize = [array count] - 1;
            DLog(@"listSize = %d",listSize);
            for( int i = 0; i < kImageIntNums; i++)
            {
                index = row *kImageIntNums +i;
                TouchImageView *imageView = [cell.imageViews objectAtIndex:i];
                
                //UIView *canCheckView = [imageView viewWithTag:kCanCheckTag];
                if(index <= listSize)
                {
                    
                    if (row > 0)
                    {
                        CGRect rect = imageView.frame;
                        rect.origin.y = 1.0;
                        imageView.frame = rect;
                    }
                    
                    KeyValueObject *item = [array objectAtIndex:index];;
                    if([item.key isEqualToString:@"add"])
                    {
                        [imageView setImage:[UIImage imageNamed:item.keyDispName]];
                    }
                    else
                    {
                        [imageView initialize];
                        NSString *imageUrl = item.key;
                        imageUrl = [NSString stringWithFormat:@"%@/photo/%@/small_%@",kHttpHost,[[FMManager sharedInstance] userId],imageUrl];
                        DLog(@"imageUrl = %@",imageUrl);
                        [imageView setImageWithURL:[NSURL URLWithString:imageUrl]
                                  placeholderImage:[UIImage imageNamed:@"touxiang.png"]];
                        
                    }
                    imageView.delegate = self;
                    imageView.tag = index + kImageViewTagBase;
                    imageView.hidden = NO;
                    
                }
                else
                {
                    imageView.hidden = YES;
                }
                
            }
            return cell;
    }
    else if( section == 1)
    {
        if(indexPath.row != 2)
        {
            static NSString *Cell1 = @"Cell1";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell1];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if(cell == nil)
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Cell1] autorelease];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.textLabel.font =[UIFont systemFontOfSize:15];
                cell.textLabel.textAlignment = UITextAlignmentLeft;
                cell.detailTextLabel.font = [UIFont systemFontOfSize:15.0];
            }
            cell.textLabel.text = [self.section1List objectAtIndex:indexPath.row];
            int row = indexPath.row;
            
            if (row == 0)
            {
                cell.tag = 939;
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                if (self.modelSecondTypeString.length > 0)
                {
                    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@:%@",self.modelFirstTypeString,self.modelSecondTypeString];
                }
                else
                {
                    cell.detailTextLabel.text = @"选择";
                }
            }
            else
            {
                if(row >1)
                {
                    row--;
                }
                int tag = kTextFieldTagBase + row;
                UIView *view = [cell viewWithTag:tag];
                if(view)
                {
                    [view removeFromSuperview];
                }
                [cell addSubview:[self.textFieldList1 objectAtIndex:(row - 1)]];
            }
            
            return cell;
        }
        else
        {
            static NSString *Cell11 = @"Cell11";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell11];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if(cell == nil)
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Cell11] autorelease];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                //cell.textLabel.textColor = [UIColor grayColor];
                cell.textLabel.font =[UIFont systemFontOfSize:15];
                cell.textLabel.textAlignment = UITextAlignmentLeft;
                cell.textLabel.text = @"性别: ";
                
                [cell addSubview:self.sVSegmentedControl];
            }
            return cell;
           
        }
        
    }
    else if( section == 2)
    {
        static NSString *Cell2_ = @"Cell2";
        TextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell2_];
        if(cell == nil)
        {
            cell = [self.inputList objectAtIndex:0];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
        
    }
    else if( section == 3)
    {
        static NSString *Cell3_ = @"Cell3";
        TextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell3_];
        if(cell == nil)
        {
            cell = [self.inputList objectAtIndex:1];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
    else if( section == 4)
    {
        static NSString *Cell4 = @"Cell4";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell4];
        if(cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Cell4] autorelease];
            //cell.textLabel.textColor = [UIColor grayColor];
            cell.textLabel.font =[UIFont systemFontOfSize:15];
            cell.textLabel.textAlignment = UITextAlignmentLeft;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.textLabel.text = [self.section4List objectAtIndex:indexPath.row];
        int index = 9 + indexPath.row;
        
        int tag = kTextFieldTagBase + index;
        UIView *view = [cell viewWithTag:tag];
        if(view)
        {
            [view removeFromSuperview];
        }
        
        [cell addSubview:[self.textFieldList1 objectAtIndex:index]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if( section == 5)
    {
        static NSString *Cell5_ = @"Cell5";
        TextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell5_];
        if(cell == nil)
        {
            cell = [self.inputList objectAtIndex:2];
        }
        return cell;
    }
    
    return nil;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && indexPath.row == 0)
    {
        RegisterKindsChooseViewController *kindsChooseCtrl = [[[RegisterKindsChooseViewController alloc] init] autorelease];
        kindsChooseCtrl.delegate = self;
        if ([self.userIdentifyRemind isEqualToString:@"3"] || [self.userIdentifyRemind isEqualToString:@"4"])
        {
            self.userIdentifyRemind = @"5";
        }
        kindsChooseCtrl.firstMenuParentId = self.userIdentifyRemind;
        [self.navigationController pushViewController:kindsChooseCtrl animated:YES];
    }
}

- (void)updateImage:(UIImage *)editedImage
{
    self.saveImage = editedImage;
    FMManager *fm = [FMManager sharedInstance];
    NSString *userId =fm.userId;
    NSString *userType = @"1";
    NSString *modelId = [self.dataDic objectForKey:@"modelsId"];
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@?userId=%@&photoType=2&userType=%@&modelsId=%@",kHttpHost,kUploadModelsAvatarstr,userId,userType,modelId];
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.timeOutSeconds = 180;
    
    NSData *imageData = UIImageJPEGRepresentation(editedImage,1.0);
    [request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"file"];
    [request setRequestMethod:@"POST"];
    request.delegate = self;
    [request startAsynchronous];
    
    mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:mHUD];
    mHUD.delegate = self;
    [mHUD show:YES];
}

-(void)updatePhoto:(NSString *)photoName
{
    NSNumber *modelIdStr = [self.dataDic objectForKey:@"modelsId"];
    NSMutableDictionary *coDic = [[[NSMutableDictionary alloc] init] autorelease];
    
    [coDic setObject:modelIdStr forKey:@"modelsId"];
    [coDic setObject:photoName forKey:@"photoName"];
    
    NSString *postStr = [coDic JSONString];
    DLog(@"postStr = %@",postStr);
    
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",kHttpHost,kUpdModelsPhotostr];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setRequestMethod:@"POST"];
    request.delegate = self;
    [request startAsynchronous];
}
-(void)deleteImageByid:(NSString *)photoName
{
    //FMManager *fm = [FMManager sharedInstance];
    //NSString *userId = fm.userId;
    //NSNumber *num = [NSNumber numberWithInt:[userId intValue]];
    NSNumber *modelsId = [self.dataDic objectForKey:@"modelsId"];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",kHttpHost,kDelModelsPhoto];

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    NSMutableDictionary *coDic = [[[NSMutableDictionary alloc] init] autorelease];
    
    [coDic setObject:modelsId forKey:@"modelsId"];
    [coDic setObject:photoName forKey:@"photoName"];
    
    NSString *postStr = [coDic JSONString];
    DLog(@"postStr = %@",postStr);
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setRequestMethod:@"POST"];
    request.delegate = self;
    [request startAsynchronous];
}

-(void)toAddImage
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.allowsEditing = NO;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentModalViewController:picker animated:YES];
    [picker release];
    //self.window.rootViewController = picker;
    //[picker release];
    
    self.library = [[[ALAssetsLibrary alloc] init] autorelease];
    self.imageEditor = [[[DemoImageEditor alloc] initWithNibName:@"DemoImageEditor" bundle:nil] autorelease];
    
    self.imageEditor.doneCallback = ^(UIImage *editedImage, BOOL canceled){
        if(!canceled) {
            
            
            [picker dismissModalViewControllerAnimated:YES];
            [self updateImage:editedImage];
            
            
            /*
            [self.library writeImageToSavedPhotosAlbum:[editedImage CGImage]
                                           orientation:editedImage.imageOrientation
                                       completionBlock:^(NSURL *assetURL, NSError *error){
                                           if (error) {
                                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error Saving"
                                                                                               message:[error localizedDescription]
                                                                                              delegate:nil
                                                                                     cancelButtonTitle:@"Ok"
                                                                                     otherButtonTitles: nil];
                                               [alert show];
                                               [alert release];
                                           }
                                       }];
                     */
        }
        else
        {
            [picker popToRootViewControllerAnimated:YES];
            [picker setNavigationBarHidden:NO animated:YES];
        }

        

    };
    

    

#if 0
    AGImagePickerController *imagePickerController = [[AGImagePickerController alloc] initWithFailureBlock:^(NSError *error) {
        //NSLog(@"Fail. Error: %@", error);
        
        if (error == nil) {
            [self.selectedPhotos removeAllObjects];
            //NSLog(@"User has cancelled.");
            [self dismissModalViewControllerAnimated:YES];
        } else {
            
            // We need to wait for the view controller to appear first.
            double delayInSeconds = 0.5;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                [self dismissModalViewControllerAnimated:YES];
            });
        }
        
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
        
    } andSuccessBlock:^(NSArray *info) {
        [self.selectedPhotos setArray:info];
        
        //添加图片
        for(ALAsset *item in info)
        {
            NSURL *url = item.defaultRepresentation.url;
            //NSLog(@"url = %@",url);
            //NSLog(@"Info: %@", info);
            UIImage *addImage = [UIImage imageWithCGImage:item.thumbnail];
            [[SDImageCache sharedImageCache] storeImage:addImage forKey:[url absoluteString]];
            {
                KeyValueObject *obj = [[[KeyValueObject alloc] init] autorelease];
                obj.canClick = YES;
                obj.isCheck = NO;
                obj.key = @"00001";
                obj.keyDispName = [url absoluteString];
                if(self.section0list && (self.section0list.count != 0))
                {
                    [self.section0list insertObject:obj atIndex:(self.section0list.count - 1)];
                }
                else {
                    self.section0list = [[[NSMutableArray alloc] init] autorelease];
                    [self.section0list addObject:obj];
                }
                
                /*
                NSURL *url = [NSURL URLWithString:obj.keyDispName];
                MWPhoto *photo = [MWPhoto photoWithURL:url];
                photo.caption = obj.key;
                [self.photos addObject:photo];
                 */
                [self.tableView reloadData];
            }
        }
        
        [self dismissModalViewControllerAnimated:YES];
        
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    }];
    
    // Show saved photos on top
    imagePickerController.shouldShowSavedPhotosOnTop = YES;
    imagePickerController.selection = self.selectedPhotos;
    
    // Custom toolbar items
    AGIPCToolbarItem *selectAll = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"全选" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
        return YES;
    }];
    AGIPCToolbarItem *flexible = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease] andSelectionBlock:nil];
    /*
     AGIPCToolbarItem *selectOdd = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"+ Select Odd" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
     return !(index % 2);
     }];
     */
    AGIPCToolbarItem *deselectAll = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
        return NO;
    }];
    imagePickerController.toolbarItemsForSelection = [NSArray arrayWithObjects:selectAll, flexible, /*selectOdd, flexible, */deselectAll, nil];
    //    imagePickerController.toolbarItemsForSelection = [NSArray array];
    //[selectOdd release];
    [flexible release];
    [selectAll release];
    [deselectAll release];
    
    //    imagePickerController.maximumNumberOfPhotos = 3;
    [self presentModalViewController:imagePickerController animated:YES];
    [imagePickerController release];
#endif
        
}
    
    
    
    
#pragma mark - TouchImageViewDelegate


-(void) clickImageView:(TouchImageView *) imageView
{
    int i = imageView.tag  - kImageViewTagBase;
    KeyValueObject *item = [self.section0list objectAtIndex:i];
    if([item.key isEqualToString:@"add"])
    {
        [self toAddImage];
    }
    else
    {
        
    }
}

-(void) viewListing:(TouchImageView *)imageView
{
    int i = imageView.tag - kImageViewTagBase;
    mSelDeleteImageIndex = i;
    KeyValueObject *item = [self.section0list objectAtIndex:i];
    [self updatePhoto:item.key];
}

//删除图片
-(void) editListing:(TouchImageView *)imageView
{
    int i = imageView.tag  - kImageViewTagBase;
    mSelDeleteImageIndex = i;
    KeyValueObject *item = [self.section0list objectAtIndex:i];
    [self deleteImageByid:item.key];
}


#pragma mark - UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    int index = textField.tag - kTextFieldTagBase;
    if(index < (self.textFieldList1.count - 1))
    {
        int i = index +1;
        UITextField *tField = [self.textFieldList1 objectAtIndex:i];
        [tField becomeFirstResponder];
    }
    else {
        [textField resignFirstResponder];
    }
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
    mCurTextField  = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self saveCurTextField:textField];
}

- (void)saveCurTextField:(UITextField *)textField
{
    if(textField)
    {
        int index = textField.tag - kTextFieldTagBase;
        DLog(@"%d",index);
        //KeyValueObject *item = [self.textFieldDataList objectAtIndex:index];
        //item.valueDispName = textField.text;
    }
}

#pragma mark - RegisterKindsChooseViewControllerDelegate

- (void)returnChoosedRegisterIdentifier:(NSMutableArray *)modelsTypeMessages
{
    if (modelsTypeMessages.count > 0)
    {
        KeyValueObject *firstItem = [modelsTypeMessages objectAtIndex:0];
        self.modelsFirstTypeRemind = firstItem.key;
        self.modelFirstTypeString = firstItem.keyDispName;
        
        if (modelsTypeMessages.count > 1)
        {
            KeyValueObject *secondItem = [modelsTypeMessages objectAtIndex:1];
            self.modelsSecondTypeRemind = secondItem.key;
            self.modelSecondTypeString = secondItem.keyDispName;
            
            for (int i=2; i<modelsTypeMessages.count; i++)
            {
                secondItem = [modelsTypeMessages objectAtIndex:i];
                self.modelsSecondTypeRemind = [NSString stringWithFormat:@"%@,%@",self.modelsSecondTypeRemind,secondItem.key];
                self.modelSecondTypeString = [NSString stringWithFormat:@"%@,%@",self.modelSecondTypeString,secondItem.keyDispName];
            }
        }
    }
    else
    {
        self.modelsFirstTypeRemind = @"";
        self.modelsSecondTypeRemind = @"";
    }
    
    if (self.modelFirstTypeString.length > 0 && self.modelSecondTypeString.length > 0)
    {
        UITableViewCell *kindCell = (UITableViewCell *)[self.tableView viewWithTag:939];
        kindCell.detailTextLabel.text = [NSString stringWithFormat:@"%@:%@",self.modelFirstTypeString,self.modelSecondTypeString];
    }
    
    DLog(@"%@:%@,%@:%@",self.modelsFirstTypeRemind,self.modelFirstTypeString,self.modelsSecondTypeRemind,self.modelSecondTypeString);
}

#pragma mark - ASIHTTPRequestDelegate

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSString *urlStr = [NSString stringWithFormat:@"%@",request.url];
    
    NSString *action = @"";
    if (urlStr.length > 0)
    {
        NSArray *array = [urlStr componentsSeparatedByString:@"/"];
        action = [array objectAtIndex:(array.count - 1)];
    }
    DLog(@"%@",action);
    
    if([action isEqualToString:kUpdateModelsInfo])
    {
        NSData *data = [request responseData];
        NSArray *array = [data objectFromJSONData];
        if(array != nil && array.count > 0)
        {
            NSString *item0 = [array objectAtIndex:0];
            if([item0 isEqualToString:kResponseOk])
            {
                //NSDictionary *datadic = [array objectAtIndex:1];
                
                if(mHUD == nil)
                {
                    mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                    [self.navigationController.view addSubview:mHUD];
                    mHUD.delegate = self;
                    [mHUD show:YES];
                }
                mHUD.mode =MBProgressHUDModeText;
                mHUD.labelText = @"保存成功";
                [mHUD hide:YES afterDelay:1.0];
                
                if (self.delegate)
                {
                    [self.delegate updateEdit2ModelDatas:self.saveDataDic];
                }
                
                
                if(self.delegate)
                {
                    [self.delegate refeashData];
                }
                
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
        else
        {
            if(mHUD == nil)
            {
                mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                [self.navigationController.view addSubview:mHUD];
                mHUD.delegate = self;
                [mHUD show:YES];
            }
            mHUD.mode =MBProgressHUDModeText;
            mHUD.labelText = @"保存失败";
            [mHUD hide:YES afterDelay:1.5];
        }
    }
    else if( [action isEqualToString:kDelModelsPhoto])
    {
        NSData *data = [request responseData];
        NSArray *array = [data objectFromJSONData];
        if(array != nil && array.count > 0)
        {
            NSString *item0 = [array objectAtIndex:0];
            if([item0 isEqualToString:kResponseOk])
            {
                [self.section0list removeObjectAtIndex:mSelDeleteImageIndex];
                
                if(mHUD == nil)
                {
                    mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                    [self.navigationController.view addSubview:mHUD];
                    mHUD.delegate = self;
                    [mHUD show:YES];
                }
                mHUD.mode =MBProgressHUDModeText;
                mHUD.labelText = @"删除成功";
                [mHUD hide:YES afterDelay:1.0];
                
                KeyValueObject *temp = [self.section0list objectAtIndex:(self.section0list.count - 1)];
                if(![temp.key isEqualToString:@"add"])
                {
                    KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
                    item.key = @"add";
                    item.keyDispName = @"addphotos.png";
                    [self.section0list addObject:item];
                }
                
                NSString *photoNameList=@"";
                if(self.section0list.count !=1)
                {
                    for(int i = 0; i< self.section0list.count -1; i++)
                    {
                        KeyValueObject *item = [self.section0list objectAtIndex:i];
                        NSString *itemStr = item.keyDispName;
                        if (i == 0)
                        {
                            photoNameList = itemStr;
                        }
                        else
                        {
                            photoNameList = [NSString stringWithFormat:@"%@;%@",photoNameList,itemStr];
                        }
                    }
                }
                [self.saveDataDic setValue:photoNameList forKey:@"modelsPhotostr"];
                
                NSString *modelsPhotostring = [self.saveDataDic objectForKey:@"modelsPhotostr"];
                
                if (modelsPhotostring && modelsPhotostring.length > 0)
                {
                    NSRange range = [modelsPhotostring rangeOfString:@";"];
                    if (range.location != NSNotFound)
                    {
                        NSArray *photoArray = [modelsPhotostring componentsSeparatedByString:@";"];
                        NSString *touxiang = [photoArray objectAtIndex:0];
                        [self.saveDataDic setObject:touxiang forKey:@"modelsAvatarstr"];
                    }
                    else
                    {
                        [self.saveDataDic setObject:modelsPhotostring forKey:@"modelsAvatarstr"];
                    }
                }
                
                if (self.delegate)
                {
                    [self.delegate updateEdit2ModelDatas:self.saveDataDic];
                }
                
                if(delegate)
                {
                    [delegate refeashData];
                }
                [self.tableView reloadData];
            }
            else if([item0 isEqualToString:kResponseOk3])
            {
                if(array.count > 1)
                {
                    NSString *item1 = [array objectAtIndex:1];
                    if(mHUD == nil)
                    {
                        mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                        [self.navigationController.view addSubview:mHUD];
                        mHUD.delegate = self;
                        [mHUD show:YES];
                    }
                    mHUD.mode =MBProgressHUDModeText;
                    mHUD.labelText = item1;
                    [mHUD hide:YES afterDelay:1.0];
                }
                
            }
        }
        else
        {
            if(mHUD == nil)
            {
                mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                [self.navigationController.view addSubview:mHUD];
                mHUD.delegate = self;
                [mHUD show:YES];
            }
            mHUD.mode =MBProgressHUDModeText;
            mHUD.labelText = @"删除成功,返回结果失败";
            [mHUD hide:YES afterDelay:1.5];
        }
    }
    else if([action isEqualToString:kUpdModelsPhotostr])
    {
        NSData *data = [request responseData];
        NSArray *array = [data objectFromJSONData];
        if(array != nil && array.count > 0)
        {
            NSString *item0 = [array objectAtIndex:0];
            if([item0 isEqualToString:kResponseOk])
            {
                
                if(mHUD == nil)
                {
                    mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                    [self.navigationController.view addSubview:mHUD];
                    mHUD.delegate = self;
                    [mHUD show:YES];
                }
                mHUD.mode =MBProgressHUDModeText;
                mHUD.labelText = @"成功设置头像";
                [mHUD hide:YES afterDelay:1.0];
                
                if(self.section0list.count == 2)
                {
                    
                }
                else
                {
                    KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
                    KeyValueObject *temp = [self.section0list objectAtIndex:mSelDeleteImageIndex];
                    item.key = temp.key;
                    item.keyDispName = temp.keyDispName;
                    [self.section0list removeObjectAtIndex:mSelDeleteImageIndex];
                    [self.section0list insertObject:item atIndex:0];
                    [self.tableView reloadData];
                    
                    NSString *photoNameList=@"";
                    if(self.section0list.count !=1)
                    {
                        for(int i = 0; i< self.section0list.count -1; i++)
                        {
                            KeyValueObject *item = [self.section0list objectAtIndex:i];
                            NSString *itemStr = item.keyDispName;
                            if (i == 0)
                            {
                                photoNameList = itemStr;
                            }
                            else
                            {
                                photoNameList = [NSString stringWithFormat:@"%@;%@",photoNameList,itemStr];
                            }
                        }
                    }
                    [self.saveDataDic setValue:photoNameList forKey:@"modelsPhotostr"];
                    
                    NSString *modelsPhotostring = [self.saveDataDic objectForKey:@"modelsPhotostr"];
                    
                    if (modelsPhotostring && modelsPhotostring.length > 0)
                    {
                        NSRange range = [modelsPhotostring rangeOfString:@";"];
                        if (range.location != NSNotFound)
                        {
                            NSArray *photoArray = [modelsPhotostring componentsSeparatedByString:@";"];
                            NSString *touxiang = [photoArray objectAtIndex:0];
                            [self.saveDataDic setObject:touxiang forKey:@"modelsAvatarstr"];
                        }
                        else
                        {
                            [self.saveDataDic setObject:modelsPhotostring forKey:@"modelsAvatarstr"];
                        }
                    }
                    
                    if(self.delegate)
                    {
                        [self.delegate updateEdit2ModelDatas:self.saveDataDic];
                    }
                    
                    if(delegate)
                    {
                        [delegate refeashData];
                    }
                }
            }
        }
        else
        {
            if(mHUD == nil)
            {
                mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                [self.navigationController.view addSubview:mHUD];
                mHUD.delegate = self;
                [mHUD show:YES];
            }
            mHUD.mode =MBProgressHUDModeText;
            mHUD.labelText = @"设置头像 失败";
            [mHUD hide:YES afterDelay:1.5];
        }
    }
    else //上传图片
    {
        NSData *data = [request responseData];
        NSArray *array = [data objectFromJSONData];
        if(array != nil && array.count > 0)
        {
            NSString *item0 = [array objectAtIndex:0];
            if([item0 isEqualToString:kResponseOk])
            {
                NSString *photoId = [array objectAtIndex:1];
                
                FMManager *fm = [FMManager sharedInstance];
                
                NSString *imageUrl = [NSString stringWithFormat:@"%@/photo/%@/%@",kHttpHost,fm.userId,photoId];
                [[SDImageCache sharedImageCache] storeImage:self.saveImage forKey:imageUrl];
                
                KeyValueObject *obj = [[[KeyValueObject alloc] init] autorelease];
                obj.canClick = YES;
                obj.isCheck = NO;
                obj.key = photoId;
                obj.keyDispName = photoId;
                if(self.section0list && (self.section0list.count != 0))
                {
                    [self.section0list insertObject:obj atIndex:(self.section0list.count - 1)];
                }
                else
                {
                    self.section0list = [[[NSMutableArray alloc] init] autorelease];
                    [self.section0list addObject:obj];
                }
                
                if(self.section0list.count <= 8)
                {
                    KeyValueObject *temp = [self.section0list objectAtIndex:(self.section0list.count - 1)];
                    if(![temp.key isEqualToString:@"add"])
                    {
                        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
                        item.key = @"add";
                        item.keyDispName = @"addphotos.png";
                        [self.section0list addObject:item];
                    }
                }
                else
                {
                    [self.section0list removeObjectAtIndex:(self.section0list.count - 1)];
                }
                
                [self.tableView reloadData];
                
                NSString *photoNameList=@"";
                if(self.section0list.count !=1)
                {
                    for(int i = 0; i< self.section0list.count -1; i++)
                    {
                        KeyValueObject *item = [self.section0list objectAtIndex:i];
                        if (i == 0)
                        {
                            photoNameList = item.keyDispName;
                        }
                        else
                        {
                            photoNameList = [NSString stringWithFormat:@"%@;%@",photoNameList,item.keyDispName];
                        }
                    }
                }
                [self.saveDataDic setObject:photoNameList forKey:@"modelsPhotostr"];
                
                NSString *modelsPhotostring = [self.saveDataDic objectForKey:@"modelsPhotostr"];
                
                if (modelsPhotostring && modelsPhotostring.length > 0)
                {
                    NSRange range = [modelsPhotostring rangeOfString:@";"];
                    if (range.location != NSNotFound)
                    {
                        NSArray *photoArray = [modelsPhotostring componentsSeparatedByString:@";"];
                        NSString *touxiang = [photoArray objectAtIndex:0];
                        [self.saveDataDic setObject:touxiang forKey:@"modelsAvatarstr"];
                    }
                    else
                    {
                        [self.saveDataDic setObject:modelsPhotostring forKey:@"modelsAvatarstr"];
                    }
                }
                
                if(self.delegate)
                {
                    [self.delegate refeashData];
                    
                    [self.delegate updateEdit2ModelDatas:self.saveDataDic];
                }
                
                if(mHUD == nil)
                {
                    mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                    [self.navigationController.view addSubview:mHUD];
                    mHUD.delegate = self;
                    [mHUD show:YES];
                }
                mHUD.mode =MBProgressHUDModeText;
                mHUD.labelText = @"保存图片成功";
                [mHUD hide:YES afterDelay:1.0];
            }
        }
        else
        {
            if(mHUD == nil)
            {
                mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                [self.navigationController.view addSubview:mHUD];
                mHUD.delegate = self;
                [mHUD show:YES];
            }
            mHUD.mode =MBProgressHUDModeText;
            mHUD.labelText = @"保存图片失败";
            [mHUD hide:YES afterDelay:1.5];
        }
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    
    NSError *error = [request error];
    if (error)
    {
        if(mHUD == nil)
        {
            mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
            [self.navigationController.view addSubview:mHUD];
            mHUD.delegate = self;
            [mHUD show:YES];
        }
        mHUD.mode =MBProgressHUDModeText;
        mHUD.labelText = @"网络链接失败";
        [mHUD hide:YES afterDelay:1.5];
    }
}

#pragma mark -
#pragma mark MBProgressHUDDelegate methods

- (void)hudWasHidden:(MBProgressHUD *)hud {
	// Remove HUD from screen when the HUD was hidded
	[mHUD removeFromSuperview];
	[mHUD release];
	mHUD = nil;
}

#pragma mark -
#pragma mark UIImagePickerControllerDelegate methods
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image =  [info objectForKey:UIImagePickerControllerOriginalImage];
    NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
    
    [self.library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
        UIImage *preview = [UIImage imageWithCGImage:[asset aspectRatioThumbnail]];
        
        self.imageEditor.sourceImage = image;
        self.imageEditor.previewImage = preview;
        [self.imageEditor reset:NO];
        
        [picker pushViewController:self.imageEditor animated:YES];
        [picker setNavigationBarHidden:YES animated:NO];
        
    } failureBlock:^(NSError *error) {
        //NSLog(@"Failed to get asset from library");
    }];
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissModalViewControllerAnimated:YES];
    /*
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cancel"
                                                    message:@"Nowhere to go my friend. This is a demo."
                                                   delegate:nil
                                          cancelButtonTitle:@"Ok"
                                          otherButtonTitles: nil];
    [alert show];
    [alert release];
     */
}
@end